package org.summer.lawyer.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.lawyer.domain.SpuOrderItem;
import org.summer.lawyer.domain.bo.spuorderitem.SpuOrderItemBo;
import org.summer.lawyer.domain.vo.spuorderitem.SpuOrderItemVo;
import org.summer.lawyer.mapper.SpuOrderItemMapper;
import org.summer.lawyer.service.ISpuOrderItemService;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 订单子项Service业务层处理
 *
 * @author summer
 * @date 2024-06-22
 */
@RequiredArgsConstructor
@Service
public class SpuOrderItemServiceImpl extends ServiceImpl<SpuOrderItemMapper, SpuOrderItem>  implements ISpuOrderItemService {


    /**
     * 查询订单子项
     *
     * @param orderItemId 主键
     * @return 订单子项
     */
    @Override
    public SpuOrderItemVo selectSpuOrderItemVoById(Long orderItemId){
        return baseMapper.selectVoById(orderItemId);
    }

    /**
     * 分页查询订单子项列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 订单子项分页列表
     */
    @Override
    public TableDataInfo<SpuOrderItemVo> selectPageList(SpuOrderItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SpuOrderItem> lqw = buildQueryWrapper(bo);
        Page<SpuOrderItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的订单子项列表
     *
     * @param bo 查询条件
     * @return 订单子项列表
     */
    @Override
    public List<SpuOrderItemVo> selectSpuOrderItemVoList(SpuOrderItemBo bo) {
        LambdaQueryWrapper<SpuOrderItem> lqw = Wrappers.lambdaQuery();
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SpuOrderItem> buildQueryWrapper(SpuOrderItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SpuOrderItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, SpuOrderItem::getOrderId, bo.getOrderId());
        lqw.eq(bo.getSpuId() != null, SpuOrderItem::getSpuId, bo.getSpuId());
        lqw.like(StrUtil.isNotBlank(bo.getSpuName()), SpuOrderItem::getSpuName, bo.getSpuName());
        lqw.eq(bo.getCategoryId() != null, SpuOrderItem::getCategoryId, bo.getCategoryId());
        lqw.like(StrUtil.isNotBlank(bo.getCategoryName()), SpuOrderItem::getCategoryName, bo.getCategoryName());
        lqw.eq(bo.getUserId() != null, SpuOrderItem::getUserId, bo.getUserId());
        lqw.like(StrUtil.isNotBlank(bo.getUserName()), SpuOrderItem::getUserName, bo.getUserName());
        lqw.eq(StrUtil.isNotBlank(bo.getSpuImage()), SpuOrderItem::getSpuImage, bo.getSpuImage());
        lqw.eq(bo.getSpuPrice() != null, SpuOrderItem::getSpuPrice, bo.getSpuPrice());
        lqw.eq(bo.getSpuCount() != null, SpuOrderItem::getSpuCount, bo.getSpuCount());
        lqw.eq(bo.getSpuTotalAmount() != null, SpuOrderItem::getSpuTotalAmount, bo.getSpuTotalAmount());
        return lqw;
    }

    /**
     * 新增订单子项
     *
     * @param bo 订单子项
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SpuOrderItemBo bo) {
        SpuOrderItem add = MapstructUtils.convert(bo, SpuOrderItem.class);
        add.setCreateTime(DateUtil.date());
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOrderItemId(add.getOrderItemId());
        }
        return flag;
    }

    /**
     * 修改订单子项
     *
     * @param bo 订单子项
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SpuOrderItemBo bo) {
        SpuOrderItem update = MapstructUtils.convert(bo, SpuOrderItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SpuOrderItem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除订单子项信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public SpuOrderItemVo selectSpuOrderItemVoByOrderId(Long orderId) {
        LambdaQueryWrapper<SpuOrderItem> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SpuOrderItem::getOrderId, orderId);
        return baseMapper.selectVoOne(queryWrapper, false);
    }
}
